package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
@Slf4j
public class MemberSexController {

    @Reference
    private MemberService memberService;

    @RequestMapping("/ReportSex")
    public Result ReportSex(){
        try {
            //获取数据
            List<Map<String,Object>> sexList =memberService.findAll();

            Map<String,Object>map = new HashMap<>();
            map.put("sexList",sexList);

            List<String> sexName =new ArrayList<>();
            for (Map<String, Object> mapList : sexList) {
                String name = (String)mapList.get("name");
                sexName.add(name);
            }

            map.put("sexName",sexName);
            //构造返回值
//        Map<String, Object> map = new HashMap<>();
//        map.put("sex", sexList);
//        map.put("sexName", sexName);
            return  new Result(true, MessageConst.GET_MEMBERSEX_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConst.GET_MEMBERSEX_COUNT_REPORT_FAIL);
        }
    }
}
